package com.df.tank;

import com.df.tank.strategy.DefaultFireStrategy;
import com.df.tank.strategy.FireStrategy;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 抽象出tank类
 */

public class Tank {
    private static final int SPEED = 10;
    private int x, y;
    private Dir dir;
    private boolean moving;
    private TankFrame tankFrame = null;
    private BufferedImage image;
    public static int WIDTH = ImageMgr.goodTankD.getWidth();
    public static int HEIGHT = ImageMgr.goodTankD.getHeight();
    private boolean living = true;
    private Group group = Group.BAD;
    private Random random = new Random();
    //自身带一个  防止每次都new
    Rectangle rect = new Rectangle();
    private FireStrategy fireStrategy = DefaultFireStrategy.getInstance();

    public Tank(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;
        this.tankFrame = tankFrame;
        rect.x = x;
        rect.y = y;
        rect.width = WIDTH;
        rect.height = HEIGHT;
    }

    public void paint(Graphics g) {
        //长方形
//        g.fillRect(x, y, 50, 50);
        switch (dir) {
            case DOWN:
                image = group == Group.GOOD ? ImageMgr.goodTankD : ImageMgr.badTankD;
                break;
            case UP:
                image = group == Group.GOOD ? ImageMgr.goodTankU : ImageMgr.badTankU;
                break;
            case LEFT:
                image = group == Group.GOOD ? ImageMgr.goodTankL : ImageMgr.badTankL;
                break;
            case RIGHT:
                image = group == Group.GOOD ? ImageMgr.goodTankR : ImageMgr.badTankR;
                break;

        }
        g.drawImage(image, x, y, null);
        if (!living) {
            tankFrame.enemies.remove(this);
        }
        //敌方坦克随机开火
        if (group == Group.BAD && random.nextInt(100) > 95) {
            fire();
        }
        //随机方向
        if (group == Group.BAD && random.nextInt(100) > 95) {
            randomDir();
        }
        if (!moving) return;
        //移动
        move();
        boundCheck();
    }

    //边界控制
    private void boundCheck() {
        if (x<0) x = 0;
        if (y<30) y = 30;
        if (x > tankFrame.getWidth() - WIDTH) {
            x = tankFrame.getWidth() - WIDTH;
        }
        if (y > tankFrame.getHeight() - HEIGHT) {
            y = tankFrame.getHeight() - HEIGHT;
        }
    }

    private void randomDir() {
        moving = true;
        this.dir = Dir.values()[random.nextInt(4)];
    }

    private void move() {
        /*if (crashWithTank(this)) {
            return;
        }*/
        /*if (this.x <= 0 || this.y <= 0 || this.x >= tankFrame.getWidth() || this.y >= tankFrame.getHeight()) {
            randomDir();
            return;
        }*/
        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            default:
                break;
        }
        //更新位置
        rect.x = x;
        rect.y = y;
        if(this.group == Group.GOOD) new Thread(()->new Audio("audio/tank_move.wav").play()).start();

    }

    private boolean crashWithTank(Tank tank) {

        for (Tank enemy : tankFrame.enemies) {
            Rectangle tankRect = new Rectangle(this.x, this.y, this.WIDTH, this.HEIGHT);
            Rectangle enemyRect = new Rectangle(enemy.getX(), enemy.getY(), enemy.WIDTH, enemy.HEIGHT);
            if (tankRect.intersects(enemyRect)) {
                return true;
            }
        }
        return false;
    }
    public void fire() {
//        int bx = this.x + WIDTH / 2 - Bullet.WIDTH / 2;
//        int by = this.y + HEIGHT / 2 - Bullet.HEIGHT / 2;
//        this.tankFrame.bulletList.add(new Bullet(bx, by, this.dir,this.group, this.tankFrame));
        fireStrategy.reload(this);
        if(this.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
    }

    public void die() {
        living = false;
        new Explode(this.x, this.y, this.tankFrame);
    }
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public FireStrategy getFireStrategy() {
        return fireStrategy;
    }

    public void setFireStrategy(FireStrategy fireStrategy) {
        this.fireStrategy = fireStrategy;
    }

    public Rectangle getRect() {
        return rect;
    }

    public void setRect(Rectangle rect) {
        this.rect = rect;
    }

    public TankFrame getTankFrame() {
        return tankFrame;
    }

    public void setTankFrame(TankFrame tankFrame) {
        this.tankFrame = tankFrame;
    }
}
